﻿using AutoMapper;
using MediatR;
using MySqlX.XDevAPI.Common;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using Return.Enum;
using SlaughterItem.Common.Infrastructures.Interfaces;
using THE_WIND_RISES.Common.Domans.BasicInformation.warehouses;
using THE_WIND_RISES.Common.Domans.RBAC;

namespace RBAC_Set.API.Application.Commands
{
    public class AddPermissionHandler : IRequestHandler<AddPermissionCommand, THE_WIND_RISES.Common.Dtos.Result>
    {
        public IBaseRepository<Permission> PermissionRep { get; set; }
        public IMapper mapper { get; set; }
        public ILogger<AddPermissionHandler> logger;

        public AddPermissionHandler(IBaseRepository<Permission> permissionRep, IMapper mapper, ILogger<AddPermissionHandler> logger)
        {
            PermissionRep = permissionRep;
            this.mapper = mapper;
            this.logger = logger;
        }

        /// <summary>
        /// 添加权限信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<THE_WIND_RISES.Common.Dtos.Result> Handle(AddPermissionCommand request, CancellationToken cancellationToken)
        {
            var result = new THE_WIND_RISES.Common.Dtos.Result() { Code = (int)CommonErrorCodes.Added };
            try
            {
                Console.WriteLine("开始添加权限" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                Console.WriteLine(JsonConvert.SerializeObject(request));
                var AddData = mapper.Map<Permission>(request);
                string ShowSql = $"select * from Permission where PermissionName = '{request.PermissionName}'";
                Console.WriteLine(ShowSql);
                var res = PermissionRep.GetAll(ShowSql).Count();
                if (res > 0)
                {
                    result.Code = (int)CommonErrorCodes.Added;
                    result.Data = null;
                    result.Msg = "添加权限信息失败，权限信息重复";
                    logger.LogError("添加权限信息失败，权限信息重复");
                    return result;
                }
                string sql = "insert into Permission values(@PermissionId,@PermissionCoding,@PermissionName,@PermissionState,@IsDelete,@URL,@Handle,@HandleContext,@HandleTime,@Pid);";
                var AddNum = PermissionRep.Add(sql, AddData);
                Console.WriteLine(sql);
                if (AddNum > 0)
                {
                    Console.WriteLine("添加权限成功");
                    result.Code = (int)CommonErrorCodes.Success;
                    result.Data = null;
                    result.Msg = "添加权限信息成功";
                    logger.LogError("添加权限信息成功");
                    return result;
                }
            }
            catch (Exception)
            {
                logger.LogError("添加权限信息失败，权限信息重复");
                throw;
            }
            result.Msg = "添加失败";
            return result;
        }
    }
}
